package com.coder520.sync_learning.demo.ThreadPool01;

import java.util.concurrent.TimeUnit;

public class InternalTask implements Runnable{
    private final RunnaleQueue runnaleQueue;

    private volatile boolean running=true;

    public InternalTask(RunnaleQueue runnaleQueue) {
        this.runnaleQueue = runnaleQueue;
    }

    @Override
    public void run() {
        //如果当前任务为running并且没有被中断，则其将不断从queue中获取runnable，然后执行run方法
        while (running&&!Thread.currentThread().isInterrupted()){
            try {
                Runnable task=runnaleQueue.task();
                TimeUnit.SECONDS.sleep(1);
                task.run();
            }catch (InterruptedException e){
                running=false;
                break;
            }
        }
    }

    public  void stop(){
        this.running=false;
    }
}
